package dao.iml;

import base.StatusCode;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Course;
import utils.DruidUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    @Override
    public List<Course> findCourseList() {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select id,course_name,price,sort_num,status from course where is_del = ?";
            List<Course> courseList = qr.query(sql, new BeanListHandler<Course>(Course.class), 0);
            return courseList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String course_name,String status) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            StringBuffer sb = new StringBuffer("select id,course_name,price,sort_num,status from course " +
                    "where 1 = 1 ");
            List<Object> list = new ArrayList<>();
            if (course_name != null && course_name != ""){
                sb.append("and course_name like ?");
                course_name = "%" + course_name + "%";
                list.add(course_name);
            }
            if (status != null && status != ""){
                sb.append("and status = ?");
                int i = Integer.parseInt(status);
                list.add(i);
            }
            List<Course> courseList = qr.query(sb.toString(), new BeanListHandler<Course>(Course.class), list.toArray());
            return courseList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int courseSalesInfo(Course course) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "insert into course(id,course_name,brief,teacher_name,teacher_info" +
                    ",preview_first_field,preview_second_field,discounts,price," +
                    "price_tag,share_image_title,share_title,share_description,course_description,course_img_url,create_time,update_time" +
                    ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Object[] objects = {course.getId(),course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info()
            ,course.getPreview_first_field(),course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),course.getPrice_tag()
            ,course.getShare_image_title(),course.getShare_title(),course.getShare_description(),course.getCourse_description(),course.getCourse_img_url()
            ,course.getCreate_time(),course.getUpdate_time()};
            int update = qr.update(sql,objects);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course findCourseById(int id) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from course where id = ?";
            Course course = qr.query(sql, new BeanHandler<Course>(Course.class), id);
            return course;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateCourse(Course course) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course set course_name = ?,brief = ?,teacher_name = ? where id = ?";
            Object[] objects = {course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getId()};
            int row = qr.update(sql,objects);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public String updateCourseStatus(int id ,int status) {
        try {
            Course courseUpdate = findCourseById(id);
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course set status = ? where id = ?";
            int row = qr.update(sql, status, id);
            if (row > 0 ){
                //修改成功！
                String result = StatusCode.SUCCESS.toString();
                return result;
            }else {
                String result = StatusCode.FAIL.toString();
                return result;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }


    }
}
